package com.itheima.controller;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.POIUtils;
import org.apache.poi.util.StringUtil;
import org.apache.zookeeper.data.Id;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author nglsk
 * @version v1.0
 * @date 2019/11/25 11:41
 * @description TODO
 **/

@RestController
@RequestMapping("/ordersetting")
public class OrderSettingController {

    @Reference
    OrderSettingService orderSettingService;

    /**
     * 根据预约时间设置可预约人数
     *
     * @param orderSetting
     * @return
     */
    @RequestMapping("/setNumberByDate")
    public Result setNumberByDate(@RequestBody OrderSetting orderSetting) {
        try {
            orderSettingService.setNumberByDate(orderSetting);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.success(MessageConstant.ORDERSETTING_FAIL);
        }
        return Result.success(MessageConstant.ORDERSETTING_SUCCESS);
    }

    private boolean isNotEmpty(Object o) {
        if (o == null) {
            return false;
        }
        if (o instanceof String) {
            String o1 = (String) o;
            if ("".equals(o1) || " ".equals(o1)) {
                return false;
            }
        }

        return true;
    }


    @RequestMapping("/upload")
    public Result upload(MultipartFile excelFile) throws IOException {
        List<String[]> dataList = POIUtils.readExcel(excelFile);

        List<OrderSetting> orderSettings = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(dataList)) {
            for (String[] strings : dataList) {
                if (strings.length != 2) {
                    continue;
                }
                String dateStr = strings[0];
                String numberStr = strings[1];
                if (isNotEmpty(dateStr) && isNotEmpty(numberStr)) {
                    OrderSetting orderSetting = new OrderSetting();
                    orderSetting.setOrderDate(DateUtil.parse(dateStr, "yyyy/MM/dd"));
                    orderSetting.setNumber(Integer.valueOf(numberStr));
                    orderSettings.add(orderSetting);
                }
            }
        }

        try {
            orderSettingService.setNumberByDateBatch(orderSettings);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error(MessageConstant.UPLOAD_FAIL);
        }
        return Result.success(MessageConstant.UPLOAD_SUCCESS);
    }

}
